package com.jxs.android.ges;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.util.MemoryUtil;
import com.jxs.android.util.StringUtils;
import com.jxs.android.weiget.BuAlertDailog;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.TopBar;
import com.jxs.android.ges.R;

@SuppressLint("HandlerLeak")
public class LaucherSystemMore_FeedBack extends AbstractActivity {
	private BuLoadingDailog buLoadingDailog;

	private Button sumbit;
	private EditText text;
	private EditText phone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.bu_sys_more_feed_back);
		sumbit = (Button) findViewById(R.id.sumbit);
		text = (EditText) findViewById(R.id.bu_more_feedback_contact_input);
		phone = (EditText) findViewById(R.id.bu_more_feedback_contact_number);
		initLoadingView();
		sumbit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (StringUtils.isEmpety(phone.getText().toString())) {
					errorMsgPop(R.string.bu_more_suggust_number_error);
					return;
				}
				if (StringUtils.isEmpety(text.getText().toString())) {
					errorMsgPop(R.string.bu_more_feedback_contact_error);
					return;
				}
				sumbit(text.getText().toString(), phone.getText().toString());
			}
		});

	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.hindSpinnerBtn();
		topBarReturn(topBar);
		topBar.setTitle(R.string.bu_more_suggust);
	}

	private void initLoadingView() {
		buLoadingDailog = new BuLoadingDailog(this);
		buLoadingDailog.setLoadingListener(new BuLoadingDailog.LoadingListener() {

			@Override
			public void okLogic(Dialog dialog) {
				dialog.dismiss();
			}

			@Override
			public String getOkText(Context context) {
				return getString(R.string.prompt_confirm);
			}
		});
	}

	private void sumbit(final String content, final String phone) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				String wsdl_url = GesApplication.WSDL_URL;// wsdl
				String namespace = GesApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "sendUserFeedback";// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", GesApplication.getUserInfo().getTicket());
				request.addProperty("content", content);
				request.addProperty("phone", phone);
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						// SoapObject response = (SoapObject)
						// envelope.getResponse();
						paser(envelope.getResponse().toString());
					}

					@Override
					public void error(String message, Exception e) {
						Message msg = new Message();
						msg.what = ERROR_MSG;
						msg.obj = message;
						handler.sendMessage(msg);
					}
				});
			}
		}).start();
	}

	private void paser(String stauts) {
		if ("0".equals(stauts)) {// 0:提交失败
			handler.sendEmptyMessage(SUCCS_MSG);
		} else if ("1".equals(stauts)) {// 1: 提交成功
			handler.sendEmptyMessage(ERROR_MSG);
		} else if ("-1".equals(stauts)) {// 1: 提交成功
			handler.sendEmptyMessage(TICKET_ERROR);
		}
	}

	private int SUCCS_MSG = 0x110;
	private int ERROR_MSG = 0x112;
	private int TICKET_ERROR = 0x113;

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (MemoryUtil.isCurrentActivity(LaucherSystemMore_FeedBack.class)) {
				if (msg.what == SUCCS_MSG) {
					errorMsgPop(R.string.bu_more_suggust_success, new BuAlertDailog.IPhoneAlertDailog() {
						@Override
						public void okLogic(Dialog dialog) {
							finish();
						}

						@Override
						public String getOkText(Context context) {
							return getString(R.string.prompt_confirm);
						}
					});
				} else if (msg.what == ERROR_MSG) {
					errorMsgPop(R.string.bu_more_suggust_fail);
				} else if (msg.what == TICKET_ERROR) {
					errorMsgToLogin("登陆已过期，请重新登陆");
				}
			}
		}

	};

}